namingGlobals
Variables Timm Mar10 Sandbox Revised

Naming Globals

Problem
=======

As discussed in [?staticVars Using Static Variables], it is possible to hide variables from the AWK global space
by declaring extra arguments in a function header.

If you accidently forget to do
that, then variables that should have been local go "rogue" and
pollute the global space, with bizarre and confusing consequences.

A simple naming convention lets you quickly find rogue locals.

+ Locals start with lowercase
+ Your our globals are MixedCase starting with upper case
+ This  leaves AWK's globals as ALL UPPER case

Now we can find the rogues by studying the output from PGAWK's _dump-variables_ command:

  pgawk --dump-variables=$HOME/tmp/vars.out  -f yourcode.awk
  egrep -v '^[A-Z]+:' $HOME/tmp/vars.out

Hint
----

To fund where the rogue was set, the following trick works well in many cases. 

If the rogue was, say, "_x_" then the following regular expression should do the trick:

  egreg 'x[ \t]*=' yourcode.awk

Author
======

Tim Menzies

